import pygame


class Ship:
    def __init__(self, screen):
        """
        初始化飞船并设置其初始位置
        :param screen:
        """
        self.screen = screen
        # 加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # 将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # 在飞船的属性center中存储小数值，更细致的控制移动速度
        self.center = float(self.rect.centerx)

        # 移动标志
        self.moving_up = False
        self.moving_down = False
        self.moving_right = False
        self.moving_left = False

    def blitShip(self):
        """
        在指定位置绘制飞船
        :return:
        """
        self.screen.blit(self.image, self.rect)

    def move(self, gameSetting):
        """
        根据移动标志调整飞船的位置
        只允许在可视界面内移动
        :return:
        """
        if self.moving_right:
            if self.rect.centerx < gameSetting.screen_width:
                self.center += gameSetting.ship_speed_factor
                self.rect.centerx = self.center
        if self.moving_left:
            if self.rect.centerx > 0:
                self.center -= gameSetting.ship_speed_factor
                self.rect.centerx = self.center
        if self.moving_up:
            if self.rect.bottom > 50:
                self.rect.bottom -= 1
        if self.moving_down:
            if self.rect.bottom < gameSetting.screen_height:
                self.rect.bottom += 1
